<template>
	<!-- mine/pages/invoiceDetails/invoiceDetails.wxml -->
	<view class="invoice" :style="objColor">
		<!-- 发票类型 -->
		<view class="genre">
			<view class="title">发票类型</view>
			<view class="content">
				<view class="content-hang" style="height:140rpx;">
					<view class="content-left">开票方式</view>
					<view class="content-right">
						<!-- <van-radio-group :value="invoiceRiseTypeEnum" @change="onChange">
								<van-radio name="1" class="radio">企业单位</van-radio>
								<van-radio name="2" class="radio">个人/非企业单位</van-radio>
						</van-radio-group> -->
						<u-radio-group
							:value="invoiceRiseTypeEnum2"
							placement="column"
							:iconSize="20"
						>	
							<u-radio
								v-if="invoiceType.includes('1')"
								size=35
								label="电子普通发票"
								:customStyle="{margin: '8rpx',marginTop: '16rpx'}"
								name="1"
								@change="onChange2"
							></u-radio>
							
							<u-radio
								v-if="invoiceType.includes('2')"
								size=35
								:customStyle="{margin: '8rpx'}"
								label="纸质专用发票"
								name="2"
								@change="onChange2"
							></u-radio>
							
							<u-radio
								v-if="invoiceType.includes('3')"
								size=35
								:customStyle="{margin: '8rpx'}"
								label="电子专用发票"
								name="3"
								@change="onChange2"
							></u-radio>
							
							<!-- <u-radio
								:customStyle="{marginLeft: '8px'}"
								size=35
								label="专用发票"
								name="2"
								@change="onChange2"
							></u-radio> -->
							
						</u-radio-group>
					</view>
				</view>
				<view class="content-hang">
					<view class="content-left">抬头类型</view>
					<view class="content-right">
						<!-- <van-radio-group :value="invoiceRiseTypeEnum" @change="onChange">
								<van-radio name="1" class="radio">企业单位</van-radio>
								<van-radio name="2" class="radio">个人/非企业单位</van-radio>
						</van-radio-group> -->
						<u-radio-group
							:value="invoiceRiseTypeEnum"
							placement="row"
							:iconSize="20"
						>
							<u-radio
								size=35
								:customStyle="{}"
								label="企业单位"
								name="1"
								@change="onChange"
							></u-radio>
							
							<u-radio
								v-if="invoiceRiseTypeEnum2 == '1'"
								size=35
								:customStyle="{marginLeft: '8px'}"
								label="个人/非企业单位"
								name="2"
								@change="onChange"
							></u-radio>
						</u-radio-group>
					</view>
				</view>
				<view class="content-hang">
					<view class="content-left">
						<text style="color: red">*</text>
						发票抬头
					</view>
					<view class="content-right">
						<input placeholder="请输入抬头" class="content-ipt" @input="invoiceRiseInput" :value="invoiceRise" />
					</view>
				</view>
				<view class="content-hang">
					<view class="content-left">
						<text v-if="invoiceRiseTypeEnum == 1" style="color: red">*</text>
						税号
					</view>
					<view class="content-right">
						<input placeholder="请输入税号" class="content-ipt" @input="dutyParagraphInput" :value="dutyParagraph" />
					</view>
				</view>
				<view class="content-hang">
					<view class="content-left">发票内容</view>
					<view class="content-right">
						<view class="content-test">充电电费&充电服务费</view>
					</view>
				</view>
				<view class="content-hang">
					<view class="content-left">发票金额</view>
					<view class="content-right">
						<view class="money">
							{{ sumPrice / 100 }}
							<text>元</text>
						</view>
					</view>
				</view>
				<view class="content-hang">
					<view class="content-left">备注信息</view>
					<view class="content-right">
                        <textarea
																class="content-test conter-textarea"
																@blur="bindTextAreaBlur"
																auto-height
																placeholder="请输入备注信息"
																maxlength="-1"
																:value="invoiceApplyingNote"
																@input="invoiceApplyingNoteInput"
												/>
					</view>
				</view>
			</view>
		</view>
		<!-- 接收信息 -->
		<view class="genre">
			<view class="title">接收信息</view>
			<view class="content last-conter">
				<view class="content-hang" v-if="invoiceRiseTypeEnum2 === '1' || invoiceRiseTypeEnum2 === '3'">
					<view class="content-left">电子邮箱</view>
					<view class="content-right">
						<input placeholder="请输入邮箱" class="content-ipt" @input="emailInput" :value="email" />
					</view>
				</view>
				
				<view class="content-hang" v-if="invoiceRiseTypeEnum2 === '2'">
					<view class="content-left">收件人</view>
					<view class="content-right">
						<input placeholder="请输入收件人" class="content-ipt" v-model="receiver"/>
					</view>
				</view>
				
				<view class="content-hang" v-if="invoiceRiseTypeEnum2 === '2'">
					<view class="content-left">收件人地址</view>
					<view class="content-right">
						<input placeholder="请输入收件人地址" class="content-ipt" v-model="receiverAddress"/>
					</view>
				</view>
				
				<view class="content-hang" v-if="invoiceRiseTypeEnum2 === '2'">
					<view class="content-left">收件人电话</view>
					<view class="content-right">
						<input placeholder="请输入收件人电话" class="content-ipt" v-model="receiverTel"/>
					</view>
				</view>
				
				<view class="content-hang" v-if="invoiceRiseTypeEnum2 === '2' || invoiceRiseTypeEnum2 === '3'">
					<view class="content-left">开户行</view>
					<view class="content-right">
						<input placeholder="请输入开户行" class="content-ipt" v-model="openingBank"/>
					</view>
				</view>
				
				<view class="content-hang" v-if="invoiceRiseTypeEnum2 === '2' || invoiceRiseTypeEnum2 === '3'">
					<view class="content-left">开户行账号</view>
					<view class="content-right">
						<input placeholder="请输入开户行账号" class="content-ipt" v-model="openingBankAccount"/>
					</view>
				</view>
				
				<view class="content-hang" v-if="invoiceRiseTypeEnum2 === '2' || invoiceRiseTypeEnum2 === '3'">
					<view class="content-left">注册电话</view>
					<view class="content-right">
						<input placeholder="请输入注册电话" class="content-ipt" v-model="phone"/>
					</view>
				</view>
				
				<view class="content-hang" v-if="invoiceRiseTypeEnum2 === '2' || invoiceRiseTypeEnum2 === '3'">
					<view class="content-left">注册地址</view>
					<view class="content-right">
						<input placeholder="请输入注册地址" class="content-ipt" v-model="address"/>
					</view>
				</view>
			</view>
		</view>
		<!-- <view class="genre-text" v-if="(sumPrice / 100)<1000">备注：电子发票开具后会发往您的电子邮箱，请注意查收(一 般3个工作日内完成)</view>
		<view class="genre-text" v-else>备注：若所选订单金额超过1000元的，建议您分多次申请开票，每次开票金额不超过1000元。</view> -->
		
		<view v-if="invoiceConfig.notice" class="genre-text">备注：{{invoiceConfig.notice}}</view>
		<view class="genre-btn" @tap="parseEventDynamicCode($event, loding ? '' : 'submit')">确认提交</view>
	</view>
</template>

<script>
	'use strict';

	var https_1 = require('./../../../api/https.js');
	const app = getApp()
	export default {
		components: {
		},
		data() {
			return {
				loding: false,
				invoiceRiseTypeEnum: '1',
				invoiceRiseTypeEnum2: '1',
				invoiceRise: '',
				dutyParagraph: '',
				invoiceApplyingNote: '',
				email: '',
				receiver:'',
				receiverAddress: '',
				receiverTel: '',
				openingBank: '', 
				openingBankAccount: '', 
				phone: '', 
				address: '',
				sumPrice: '',
				invoiceItem1: '',

				// 发票内容2
				invoiceItem2: '',

				ids: '',
				show: false,
				tabIndex: 0,
				invoiceType:[],
				invoiceConfig:{}
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function (options) {
			//   console.log(options)
			this.ids= options.billIds

			if (options.sumPrice) {
				this.sumPrice= options.sumPrice
			}
			this.getBySellerId()
		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function () {
			this.getQueryInvoiceRecently()
		},
		methods: {
			getBySellerId: function() {
				var that = this;
				https_1.getBySellerId(app.globalData.sellerId).then(function(res) {
					console.log(res,'发票配置接口')
					if (res.code == 0) {
						if(res.data.invoiceType){
							that.setData({
								invoiceConfig:res.data || {},
								invoiceType:res.data.invoiceType.split(',')
							});
						}
						
					}
				});
			},
			//获取发票个人信息
			getQueryInvoiceRecently: function () {
				var that = this;
				https_1
					.queryInvoiceRecently({
						invoiceRiseTypeEnum: that.invoiceRiseTypeEnum,
					})
					.then(function (res) {
						console.log(res)
						if (res.code == 0 && res.data) {
							that.invoiceRise = res.data.invoiceRise
							that.dutyParagraph = res.data.dutyParagraph
							that.invoiceApplyingNote = res.data.invoiceApplyingNote
							that.email = res.data.email
						}else{
							console.log('null----')
							that.invoiceRise = ''
							that.dutyParagraph = ''
							that.invoiceApplyingNote = ''
							that.email = ''
						}
					})
					['catch'](function (err) {});
			},

			onChange: function (event) {
				this.invoiceRiseTypeEnum= event

				//切换开票类型时获取发票个人信息
				this.getQueryInvoiceRecently()
			},
			
			onChange2: function (event) {
				this.invoiceRiseTypeEnum2= event
				if(event == '2' || event == '3'){
					this.invoiceRiseTypeEnum = '1'
				}
				//切换开票类型时获取发票个人信息
				this.getQueryInvoiceRecently()
			},

			bindTextAreaBlur: function (e) {
				// console.log(e.detail.value)
			},

			invoiceRiseInput: function (e) {
				var value = e.detail.value;
				this.invoiceRise=value
			},

			dutyParagraphInput: function (e) {
				var value = e.detail.value;
				this.dutyParagraph= value
			},

			invoiceApplyingNoteInput: function (e) {
				var value = e.detail.value;
				this.invoiceApplyingNote= value
			},

			emailInput: function (e) {
				var value = e.detail.value;
				this.email=value
			},

			submit: function () {
				var that = this;
				var tmp = this;
				var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

				if (!tmp.invoiceRise) {
					uni.showToast({
						title: '请输入发票抬头',
						icon: 'none',
						duration: 2000
					});
				} else if (!tmp.dutyParagraph && tmp.invoiceRiseTypeEnum == '1') {
					uni.showToast({
						title: '请输入税号',
						icon: 'none',
						duration: 2000
					});
				} else if (!tmp.email && tmp.invoiceRiseTypeEnum2=='1') {
					uni.showToast({
						title: '请输入电子邮箱',
						icon: 'none',
						duration: 2000
					});
				} else if (!reg.test(tmp.email)) {
					uni.showToast({
						title: '请输入正确邮箱',
						icon: 'none',
						duration: 2000
					});
				} else if (!tmp.receiver && tmp.invoiceRiseTypeEnum2=='2') {
					uni.showToast({
						title: '请输入收件人',
						icon: 'none',
						duration: 2000
					});
				} else if (!tmp.receiverAddress && tmp.invoiceRiseTypeEnum2=='2') {
					uni.showToast({
						title: '请输入收件人地址',
						icon: 'none',
						duration: 2000
					});
				} else if (!tmp.receiverTel && tmp.invoiceRiseTypeEnum2=='2') {
					uni.showToast({
						title: '请输入收件人电话',
						icon: 'none',
						duration: 2000
					});
				} else if (!tmp.openingBank && tmp.invoiceRiseTypeEnum2=='2') {
					uni.showToast({
						title: '请输入开户行',
						icon: 'none',
						duration: 2000
					});
				} else if (!tmp.openingBankAccount && tmp.invoiceRiseTypeEnum2=='2') {
					uni.showToast({
						title: '请输入开户行账号',
						icon: 'none',
						duration: 2000
					});
				} else if (!tmp.phone && tmp.invoiceRiseTypeEnum2=='2') {
					uni.showToast({
						title: '请输入注册电话',
						icon: 'none',
						duration: 2000
					});
				} else if (!tmp.address && tmp.invoiceRiseTypeEnum2=='2') {
					uni.showToast({
						title: '请输入注册地址',
						icon: 'none',
						duration: 2000
					});
				}else {
					this.loding= true
					
					let isZSJInvoice = null
					if(that.isZshAppInH5()){
						isZSJInvoice=0
					}
					https_1
						.applyInvoice({
							invoiceRiseTypeEnum: tmp.invoiceRiseTypeEnum,
							invoiceRise: tmp.invoiceRise,
							totalBalance: tmp.sumPrice,
							invoiceApplyingNote: tmp.invoiceApplyingNote,
							dutyParagraph: tmp.dutyParagraph,
							email: tmp.email,
							receiver: tmp.receiver,
							receiverAddress: tmp.receiverAddress,
							receiverTel: tmp.receiverTel,
							openingBank: tmp.openingBank,
							openingBankAccount: tmp.openingBankAccount,
							phone: tmp.phone,
							address: tmp.address,
							invoiceItem1: tmp.invoiceItem1,
							invoiceItem2: tmp.invoiceItem2,
							billIds: JSON.parse(tmp.ids),
							billTotal: JSON.parse(tmp.ids).length,
							isZSJInvoice,
							invoiceType:tmp.invoiceRiseTypeEnum2,
							isOffline: tmp.invoiceRiseTypeEnum2==2 ? 0 : null
						})
						.then(function (res) {
							if (res.code == 0) {
								uni.showModal({
									title: '信息提示',
									content: '开发票申请成功！',
									showCancel: false,
									success: function (res) {
										if (res.confirm) {
											// var pages = getCurrentPages();
											// var prevPage = pages[pages.length - 2];
											// prevPage.show=false,
											//     prevPage.tabIndex= 2
											uni.setStorageSync('applyInvoiceSuccess',JSON.stringify(
												{
													show: false,
													tabIndex: 2
												}
											))
											uni.navigateBack();
										}
									}
								});
							}

							if (res.code == 3) {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 2000
								});
								that.loding= false
							}
						})
						['catch'](function (err) {
						that.loding= false
						console.log(err);
					});
				}
			}
		}
	};
</script>
<style lang="less">
	@import url('./invoiceDetails.less');
</style>
